package jnBase.components
{
	import laya.display.Sprite;
	import laya.events.Event;
	import laya.maths.Point;
	import laya.maths.Rectangle;
	import laya.ui.Image;
	import laya.utils.Browser;
	
	import ui.tools.capBarUI;
	
	public class CaptureRect extends Sprite
	{
		private var bg:Sprite;
		private var drawRect:Sprite;
		private var drawRectLine:Sprite;
		private var lastPoint:Point;
		private var isDown:Boolean;
		
		private var rectline:Sprite;
		
		private var maskArea:Sprite;
		
		private var lt:Sprite;
		
		private var lc:Sprite;
		
		private var lb:Sprite;
		
		private var rt:Sprite;
		
		private var rc:Sprite;
		
		private var rb:Sprite;
		
		private var tc:Sprite;
		
		private var cb:Sprite;
		private var capbar:capUi;
		private var isScale:Boolean;
		private var mouseCursor:Image;
		private var _wid:Number;
		private var _heg:Number;
		public function CaptureRect($wid:Number,$heg:Number)
		{
			super();
			_wid=$wid;
			_heg=$heg;
		}
		
		override public function onAwake():void
		{
			// TODO Auto Generated method stub
			super.onAwake();
			bg=new Sprite();
			this.addChild(bg);
			bg.size(_wid,_heg+100);
//			bg.cacheAs="bitmap";
			var ms:Sprite=new Sprite();
			ms.graphics.drawRect(0,0,_wid,_heg,"#ff0000");
			bg.mask=ms;
			
			
			mouseCursor||=new Image();
			mouseCursor.anchorX=0.5;
			mouseCursor.anchorY=0.5;
			this.addChild(mouseCursor);
			mouseCursor.mouseEnabled=false;
			capbar=new capUi();
			capbar.on("onAwake",this,function(){
				trace("onAwake");
				capbar.btn_cancle.on(Event.CLICK,this,cancleHandler);
				capbar.btn_sum.on(Event.CLICK,this,sumHandler);
				
			});
			this.addChild(capbar);
			
			lastPoint=new Point();
			bg.on(Event.MOUSE_DOWN,this,mouseDown);
			bg.on(Event.MOUSE_OUT,this,mouseUp);
			bg.on(Event.MOUSE_UP,this,mouseUp);
			bg.on(Event.MOUSE_MOVE,this,mouseMove);
			this.once(Event.REMOVED,this,removed);
		}
		
		override public function onEnable():void
		{
			// TODO Auto Generated method stub
			super.onEnable();
			bg.scrollRect=new Rectangle(0,0,Browser.clientWidth,Browser.clientHeight);
			
			//绘制遮罩区，含透明度，可见游戏背景
			maskArea = new Sprite();
			maskArea.alpha = 0.8;
			maskArea.graphics.drawCircle(_wid/2, _wid/2-200, _wid/2+200, "#000000");	
			bg.addChild(maskArea);
			
			
			drawRect = new Sprite();
			bg.addChild(drawRect);
//			drawRect.blendMode = "destination-out";
			drawRect.graphics.drawRect(0,0,1150,800,"#000000","ff0000");
			drawRect.size(1150,800);
			drawRect.pos(100,50);
			drawRect.on(Event.MOUSE_OVER,this,mmHandler);
			drawRect.on(Event.MOUSE_OUT,this,mmHandler);
			capbar.pos(drawRect.x+drawRect.width-capbar.width,drawRect.y+drawRect.height);
			
			
			/*设置边框线*/
			rectline=new Sprite();
			bg.addChild(rectline);
		
			drawRectLine=new Sprite();
			bg.addChild(drawRectLine);
			//设置叠加模式
			//不知道原因  2.0的库 矩形sprite和矩形sprite不能设置擦除模式，无效，必须是两种不同的形状才可以
			drawRectLine.blendMode="destination-out";
			
			/*设置8个变形点
			* lt   tc   rt
			* 
			* lc        rc
			*
			* lb   cb   rb
			*/
			lt=getCircle();
			lc=getCircle();
			lb=getCircle();
			rt=getCircle();
			rc=getCircle();
			rb=getCircle();
			tc=getCircle();
			cb=getCircle();
			lt.on(Event.MOUSE_OVER,this,mouseHandler);
			rb.on(Event.MOUSE_OVER,this,mouseHandler);
			
			lc.on(Event.MOUSE_OVER,this,mouseHandler);
			rc.on(Event.MOUSE_OVER,this,mouseHandler);
			
			tc.on(Event.MOUSE_OVER,this,mouseHandler);
			cb.on(Event.MOUSE_OVER,this,mouseHandler);
			
			lb.on(Event.MOUSE_MOVE,this,mouseHandler);
			rt.on(Event.MOUSE_MOVE,this,mouseHandler);
			
			updatePos();
		}
		
		
		private function onClear():void
		{
			// TODO Auto Generated method stub
			if(mouseCursor) mouseCursor.graphics.clear();
		}
		
		private function mouseHandler(e:Event):void
		{
			// TODO Auto Generated method stub
			if(isDown) return ;
//			mouseCursor.graphics.clear();
			switch(e.currentTarget)
			{
				case lt:
				{
					mouseCursor.skin="comp/cursor_ld.png";
//					mouseCursor.loadImage("comp/cursor_ld.png");
					mouseOverType="lt";
					break;
				}
				case rb:
				{
					mouseCursor.skin="comp/cursor_ld.png";
//					mouseCursor.loadImage("comp/cursor_ld.png");
					mouseOverType="rb";
					break;
				}
				case lc:
				{
					mouseCursor.skin="comp/cursor_lr.png";
					mouseOverType="lc";
					break ;
				}
				case rc:
				{
					mouseCursor.skin="comp/cursor_lr.png";
					mouseOverType="rc";
					break ;
				}
				case tc:
				{
					mouseCursor.skin="comp/cursor_ud.png";
					mouseOverType="tc";
					break ;
				}
				case cb:
				{
					mouseCursor.skin="comp/cursor_ud.png";
					mouseOverType="cb";
					break ;
				}
				case lb:
				{
					mouseCursor.skin="comp/cursor_dr.png";
					mouseOverType="lb";
					break ;
				}
				case rt:
				{
					mouseCursor.skin="comp/cursor_dr.png";
					mouseOverType="rt";
					break ;
				}
				default:
				{
					break;
				}
			}
		}
		private function mmHandler(e:Event):void
		{
			// TODO Auto Generated method stub
			if(isDown) return ;
			if(e.type==Event.MOUSE_OVER){
				mouseCursor.skin="comp/cursor_mm.png";
				mouseOverType="move";
			}
			else{
				mouseCursor.graphics.clear();
				mouseOverType="";
			}
		}
		private var mouseOverType:String="move";
	
		private function sumHandler():void
		{
			// TODO Auto Generated method stub
			var temp:Sprite=new Sprite();
			temp.size(Math.abs(drawRect.width),Math.abs(drawRect.height));
			temp.pos(drawRect.x,drawRect.y);
			if(drawRect.width<0){
				temp.x=drawRect.x+drawRect.width;
			}
			if(drawRect.height<0){
				temp.y=drawRect.y+drawRect.height;
			}
			event("captureRect",temp);
			this.removeSelf();
			this.destroy();
		}
		
		private function cancleHandler():void
		{
			// TODO Auto Generated method stub
			this.removeSelf();
			this.destroy();
		}
		private function removed():void
		{
			// TODO Auto Generated method stub
			bg.off(Event.MOUSE_DOWN,this,mouseDown);
			bg.off(Event.MOUSE_OUT,this,mouseUp);
			bg.off(Event.MOUSE_UP,this,mouseUp);
			bg.off(Event.MOUSE_MOVE,this,mouseMove);
		}
		private var tempDrawRect:Rectangle;
		private function mouseDown(e:Event):void
		{
			// TODO Auto Generated method stub
			trace("mouseDown");
			tempDrawRect||=new Rectangle();
			tempDrawRect.width=drawRect.width;
			tempDrawRect.height=drawRect.height;
			tempDrawRect.x=drawRect.x;
			tempDrawRect.y=drawRect.y;
			isDown=true;
			lastPoint.setTo(mouseX,mouseY);
		}
		private function mouseMove(e:Event):void
		{
			// TODO Auto Generated method stub
			trace("move:"+isDown);
			if(isScale){
				mouseCursor.x=mouseX;
				mouseCursor.y=mouseY;
			}
			if(!isDown){
			}
			else{
				if(isScale){
					if(mouseOverType=="move"){
						drawRect.x+=mouseX-lastPoint.x;
						drawRect.y+=mouseY-lastPoint.y;
					}
					else{
						drawRect.graphics.clear();
						drawRectLine.graphics.clear();
						rectline.graphics.clear();
						switch(mouseOverType)
						{
							case "rc"://右缩放
							{
								tempDrawRect.width+=mouseX-lastPoint.x;
								break;
							}
							case "lc"://左缩放
							{
								tempDrawRect.width-=mouseX-lastPoint.x;
								tempDrawRect.x+=mouseX-lastPoint.x;
								break;
							}
							case "tc"://上缩放
							{
								tempDrawRect.height-=mouseY-lastPoint.y;
								tempDrawRect.y+=mouseY-lastPoint.y;
								break ;
							}
							case "cb"://下缩放
							{
								tempDrawRect.height+=mouseY-lastPoint.y;
								break ;
							}
							case "lt"://左上
							{
								tempDrawRect.width-=mouseX-lastPoint.x;
								tempDrawRect.x+=mouseX-lastPoint.x;
								tempDrawRect.height-=mouseY-lastPoint.y;
								tempDrawRect.y+=mouseY-lastPoint.y;
								break;
							}
							case "lb"://左下
							{
								tempDrawRect.width-=mouseX-lastPoint.x;
								tempDrawRect.x+=mouseX-lastPoint.x;
								tempDrawRect.height+=mouseY-lastPoint.y;
								break;
							}
							case "rt"://右上
							{
								tempDrawRect.width+=mouseX-lastPoint.x;
								tempDrawRect.height-=mouseY-lastPoint.y;
								tempDrawRect.y+=mouseY-lastPoint.y;
								break;
							}
							case "rb"://右下
							{
								tempDrawRect.width+=mouseX-lastPoint.x;
								tempDrawRect.height+=mouseY-lastPoint.y;
								break;
							}
							default:
							{
								break;
							}
						}
//						drawRect.blendMode="destination-out";
						drawRect.graphics.drawRect(0,0,tempDrawRect.width,tempDrawRect.height,"#000000",null);
						drawRect.size(tempDrawRect.width,tempDrawRect.height);
						drawRect.pos(tempDrawRect.x,tempDrawRect.y);
					}
					lastPoint.x=mouseX;
					lastPoint.y=mouseY;
				}
					
				else{
					drawRect.graphics.clear();
					drawRectLine.graphics.clear();
					rectline.graphics.clear();
//					drawRect.blendMode="destination-out";
					drawRect.graphics.drawRect(0,0,mouseX-lastPoint.x,mouseY-lastPoint.y,"#000000",null);
					drawRect.pos(lastPoint.x,lastPoint.y);
					drawRect.size(mouseX-lastPoint.x,mouseY-lastPoint.y);
				}
				
				
				updatePos();
			}
			
		}
		private var pointWid:int=6;
		private var pointHeg:int=6;
		private function updatePos():void
		{
			// TODO Auto Generated method stub
			/*更新8个点*/
			lt.pos(-pointWid/2,-pointHeg/2);
			lc.pos(-pointWid/2,drawRect.height/2-pointHeg/2);
			lb.pos(-pointWid/2,drawRect.height-pointHeg/2);
			tc.pos(drawRect.width/2,-pointHeg/2);
			cb.pos(drawRect.width/2,drawRect.height-pointHeg/2);
			rt.pos(drawRect.width-pointWid/2,-pointHeg/2);
			rc.pos(drawRect.width-pointWid/2,drawRect.height/2);
			rb.pos(drawRect.width-pointWid/2,drawRect.height-pointHeg/2);
			/*更新菜单栏*/
			capbar.pos(drawRect.x+drawRect.width-capbar.width,drawRect.y+drawRect.height);
			if(drawRect.height<0){
				capbar.y=drawRect.y;
			}
			if(drawRect.width<0){
				capbar.x=drawRect.x-capbar.width;
			}
			/*更新边框线*/
			drawRectLine.graphics.drawRect(0,0,drawRect.width,drawRect.height,"#000000","#ff0000",1);
			drawRectLine.pos(drawRect.x,drawRect.y);
			rectline.graphics.drawRect(-1,-1,drawRect.width+2,drawRect.height+2,"#000000","#ff0000",1);
			rectline.pos(drawRect.x,drawRect.y);
		}
		private function mouseUp(e:Event):void
		{
			// TODO Auto Generated method stub
			if(!isDown){
				return ;
			}
			isDown=false;
			isScale=true;
			mouseCursor.skin="comp/cursor_mm.png";
			mouseOverType="move";
		}
		private function getCircle():Sprite
		{
			// TODO Auto Generated method stub
			var circle:Sprite=new Sprite();
			circle.graphics.drawRect(0,0,pointWid,pointHeg,"#ff0000");
			drawRectLine.addChild(circle);
			circle.size(pointWid,pointHeg);
			return circle;
		}
	}
}
import ui.tools.capBarUI;

class capUi extends capBarUI{
	public function capUi():void{
		
	}
	
	override public function onAwake():void
	{
		// TODO Auto Generated method stub
		super.onAwake();
		event("onAwake");
	}
	
}